<?php
namespace App\Providers\VersionTraits;

use Dcat\Admin\Models\Menu;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

trait _1_0_20_trait{

    function _v1_0_11(){
        if(!Menu::where('uri', '/settings/pageindex')->exists()){
            $admin_menu = new Menu();
            $admin_menu->parent_id =60;
            $admin_menu->order = 8;
            $admin_menu->title = '首页';
            $admin_menu->uri = '/settings/pageindex';
            $admin_menu->extension = '';
            $admin_menu->show = 1;
            $admin_menu->save();
        }

        if(!Menu::where('uri', '/extension')->exists()){
            $admin_menu = new Menu();
            $admin_menu->icon = 'fa-certificate';
            $admin_menu->parent_id = 0;
            $admin_menu->order =  46;
            $admin_menu->title = '扩展模块';
            $admin_menu->uri = '/extension';
            $admin_menu->extension = '';
            $admin_menu->show = 1;
            $admin_menu->save();
        }


        if(!Schema::hasColumn('wx_attachment', 'state')){
            Schema::table('wx_attachment', function (Blueprint $table) {
                $table->tinyInteger('state')->after('store_type')->default(1)->comment('状态（0: 待使用 1：已使用 2:已清理）');
                $table->index('state','index_2');
            });
        }

    }

    function _v1_0_12(){
        if(!Schema::hasColumn('wx_user', 'is_robot')){
            Schema::table('wx_user', function (Blueprint $table) {
                $table->tinyInteger('is_robot')->default(0)->after('is_member')->comment('is robot');
                $table->index('is_robot', 'index_17');
            });
        }
    }

    function _v1_0_13(){

        if(!Menu::where('uri', '/settings/seo')->exists()){
            $admin_menu = new Menu();
            $admin_menu->parent_id = 51;
            $admin_menu->order =  15;
            $admin_menu->title = 'SEO';
            $admin_menu->uri = '/settings/seo';
            $admin_menu->extension = '';
            $admin_menu->show = 1;
            $admin_menu->save();
        }

        if(!Schema::hasTable('wx_minicode')){
            Schema::create('wx_minicode', function (Blueprint $table) {
                $table->id();
                $table->string('app_id', 255)->comment('app_id');
                $table->string('path', 255)->comment('path');
                $table->string('scene', 255)->comment('scene');
                $table->string('code', 255)->comment('code img');
                $table->index(['app_id', 'path', 'scene'], 'index_0');
            });
        }

        if(!Schema::hasTable('wx_posts_files')){
            Schema::create('wx_posts_files', function (Blueprint $table) {
                $table->id();
                $table->string('name', 255)->comment('文件名称');
                $table->string('size', 255)->comment('文件大小');
                $table->string('url', 255)->comment('文件地址');
                $table->bigInteger('post_id')->comment('笔记id');
                $table->index('post_id', 'index_0');
                $table->softDeletes();
                $table->timestamps();
            });
        }

        if(!Schema::hasColumn('wx_posts_goods', 'type')){
            Schema::table('wx_posts_goods', function (Blueprint $table) {
                $table->tinyInteger('type')->after('goods_id')->default(0)->comment('类型（0：words 1:single）');
                $table->index('type', 'index_2');
            });
        }

    }

    function _v1_0_14(){
        if(!Schema::hasColumn('wx_user_withdrawal', 'bank_name')){
            Schema::table('wx_user_withdrawal', function (Blueprint $table) {
                $table->string('refuse_tip', 255)->after('state')->default('')->comment('异常原因');
                $table->string('bank_card', 255)->after('price')->default('')->comment('银行卡号');
                $table->string('bank_name', 255)->after('price')->default('')->comment('支行名字');
            });
        }

        if(!Schema::hasTable('wx_user_online')){
            Schema::create('wx_user_online', function (Blueprint $table) {
                $table->bigInteger('user_id')->unique('index_0')->comment('用户id')->primary();
                $table->timestamp('online')->comment('在线时间');
            });
        }

        if(!Schema::hasTable('wx_posts_sounds')){
            Schema::create('wx_posts_sounds', function (Blueprint $table) {
                $table->id();
                $table->string('name', 255)->comment('音频名称');
                $table->bigInteger('time')->comment('音频时长');
                $table->string('url', 255)->comment('音频地址');
                $table->bigInteger('post_id')->comment('笔记id');
                $table->index('post_id', 'index_0');
                $table->softDeletes();
                $table->timestamps();
            });
        }

    }

    function _v1_0_15(){
        Menu::where('uri', 'auth/extensions')->forceDelete();

        if(!Menu::where('uri', 'settings/robot')->exists()){
            $admin_menu = new Menu();
            $admin_menu->icon = 'fa-paw';
            $admin_menu->parent_id = 0;
            $admin_menu->order =  47;
            $admin_menu->title = '活跃系统';
            $admin_menu->uri = 'settings/robot';
            $admin_menu->extension = '';
            $admin_menu->show = 1;
            $admin_menu->save();
        }

    }

    function _v1_0_16(){
        if(!Schema::hasColumn('wx_posts', 'pay_content_id')){
            Schema::table('wx_posts', function (Blueprint $table) {
                $table->bigInteger('pay_content_id')->after('user_id')->default(0)->comment('付费id');
                $table->index('pay_content_id', 'index_2');
            });
        }

        if(!Schema::hasTable('wx_posts_pay_content')){
            Schema::create('wx_posts_pay_content', function (Blueprint $table) {
                $table->id();
                $table->bigInteger('post_id')->unique('index_0')->comment('笔记id');
                $table->tinyInteger('words_percent')->default(100)->comment('字数百分比');
                $table->tinyInteger('is_file')->default(0)->comment('附件付费');
                $table->tinyInteger('is_sound')->default(0)->comment('音频付费');
                $table->longText('hidden')->nullable()->comment('隐藏内容');
                $table->float('price')->default(0)->comment('价格');
                $table->tinyInteger('credit_type')->default(0)->comment('0：金币 1：余额');
                $table->softDeletes();
                $table->timestamps();
            });
        }

        if(!Menu::where('uri', 'settings/publish')->exists()){
            $admin_menu = new Menu();
            $admin_menu->parent_id = 60;
            $admin_menu->order =  9;
            $admin_menu->title = '发布';
            $admin_menu->uri = 'settings/publish';
            $admin_menu->extension = '';
            $admin_menu->show = 1;
            $admin_menu->save();
        }

        if(!Menu::where('uri', 'settings/contents')->exists()){
            $admin_menu = new Menu();
            $admin_menu->parent_id = 8;
            $admin_menu->order =  69;
            $admin_menu->title = '内容源';
            $admin_menu->uri = 'settings/contents';
            $admin_menu->extension = '';
            $admin_menu->show = 1;
            $admin_menu->save();
        }

        if(!Schema::hasTable('wx_user_pay')){
            Schema::create('wx_user_pay', function (Blueprint $table) {
                $table->id();
                $table->bigInteger('user_id')->index('index_0')->comment('用户id');
                $table->bigInteger('post_id')->index('index_1')->comment('笔记id');
                $table->bigInteger('pay_content_id')->index('index_2')->comment('pay_content_id');
                $table->index(['user_id', 'post_id', 'pay_content_id'], 'index_3');
                $table->timestamps();
            });
        }

    }

    function _v1_0_17(){
        if(!Schema::hasTable('wx_user_visit')){
            Schema::create('wx_user_visit', function (Blueprint $table) {
                $table->id();
                $table->bigInteger('user_id')->comment('用户id');
                $table->tinyInteger('type')->comment('类型（1:笔记 2:圈子 3:商品 4:用户 5:话题）');
                $table->bigInteger('object_id')->comment('目标id');
                $table->bigInteger('times')->comment('次数');
                $table->index(['user_id', 'type', 'object_id'], 'index_0');
                $table->index(['type', 'object_id'], 'index_1');
                $table->timestamps();
            });
        }
    }

    function _v1_0_18(){
        if(!Schema::hasColumn('wx_posts', 'img_count')){
            Schema::table('wx_posts', function (Blueprint $table) {
                $table->unsignedTinyInteger('img_count')->after('tags_id')->default(0)->comment('图片数量');
            });
        }
    }

    function _v1_0_19(){
        if(!Menu::where('uri', '/settings/admin')->exists()){
            $admin_menu = new Menu();
            $admin_menu->parent_id = 60;
            $admin_menu->order =  10;
            $admin_menu->title = '管理端';
            $admin_menu->uri = '/settings/admin';
            $admin_menu->extension = '';
            $admin_menu->show = 1;
            $admin_menu->save();
        }

        if(!Menu::where('uri', '/settings/circle')->exists()){
            $admin_menu = new Menu();
            $admin_menu->parent_id = 60;
            $admin_menu->order =  11;
            $admin_menu->title = '圈子';
            $admin_menu->uri = '/settings/circle';
            $admin_menu->extension = '';
            $admin_menu->show = 1;
            $admin_menu->save();
        }

        if(!Menu::where([ ['parent_id', '=', 0], ['title', '=', '实时热榜'] ])->exists()){
            $admin_menu = new Menu();
            $admin_menu->icon = 'fa-bomb';
            $admin_menu->parent_id = 0;
            $admin_menu->order =  48;
            $admin_menu->title = '实时热榜';
//                $admin_menu->uri = '';
            $admin_menu->extension = '';
            $admin_menu->show = 1;
            $admin_menu->save();
            $parent_id = $admin_menu->id;

            if(!Menu::where('uri', '/realtime/post')->exists()){
                $admin_menu = new Menu();
                $admin_menu->parent_id = $parent_id;
                $admin_menu->order =  1;
                $admin_menu->title = '笔记';
                $admin_menu->uri = '/realtime/post';
                $admin_menu->extension = '';
                $admin_menu->show = 1;
                $admin_menu->save();
            }

            if(!Menu::where('uri', '/realtime/topic')->exists()){
                $admin_menu = new Menu();
                $admin_menu->parent_id = $parent_id;
                $admin_menu->order =  2;
                $admin_menu->title = '话题';
                $admin_menu->uri = '/realtime/topic';
                $admin_menu->extension = '';
                $admin_menu->show = 1;
                $admin_menu->save();
            }

            if(!Menu::where('uri', '/realtime/circle')->exists()){
                $admin_menu = new Menu();
                $admin_menu->parent_id = $parent_id;
                $admin_menu->order =  3;
                $admin_menu->title = '圈子';
                $admin_menu->uri = '/realtime/circle';
                $admin_menu->extension = '';
                $admin_menu->show = 1;
                $admin_menu->save();
            }

            if(!Menu::where('uri', '/realtime/user')->exists()){
                $admin_menu = new Menu();
                $admin_menu->parent_id = $parent_id;
                $admin_menu->order = 4;
                $admin_menu->title = '用户';
                $admin_menu->uri = '/realtime/user';
                $admin_menu->extension = '';
                $admin_menu->show = 1;
                $admin_menu->save();
            }

            if(!Menu::where('uri', '/realtime/plate')->exists()){
                $admin_menu = new Menu();
                $admin_menu->parent_id = $parent_id;
                $admin_menu->order =  5;
                $admin_menu->title = '板块';
                $admin_menu->uri = '/realtime/plate';
                $admin_menu->extension = '';
                $admin_menu->show = 0;
                $admin_menu->save();
            }

            if(!Menu::where('uri', '/realtime/goods')->exists()){
                $admin_menu = new Menu();
                $admin_menu->parent_id = $parent_id;
                $admin_menu->order =  6;
                $admin_menu->title = '商品';
                $admin_menu->uri = '/realtime/goods';
                $admin_menu->extension = '';
                $admin_menu->show = 0;
                $admin_menu->save();
            }

        }

        if(!Schema::hasColumn('wx_posts', 'degree')){
            Schema::table('wx_posts', function (Blueprint $table) {
                $table->float('degree')->default(0)->after('weight')->comment('实时热度');
            });
        }

    }

    function _v1_0_20(){
        if(!Schema::hasColumn('wx_posts', 'device')){
            Schema::table('wx_posts', function (Blueprint $table) {
                $table->string('device', 20)->nullable()->after('posts_state')->comment('客户端');
            });
        }
        if(!Schema::hasColumn('wx_comment', 'device')){
            Schema::table('wx_comment', function (Blueprint $table) {
                $table->string('device', 20)->nullable()->after('comment_state')->comment('客户端');
            });
        }
        if(!Schema::hasColumn('wx_user_online', 'device')){
            Schema::table('wx_user_online', function (Blueprint $table) {
                $table->string('device', 20)->nullable()->after('online')->comment('客户端');
            });
        }
        if(!Schema::hasTable('wx_degree_increment')){
            Schema::create('wx_degree_increment', function (Blueprint $table) {
                $table->id();
                $table->bigInteger('object_id')->comment('目标id');
                $table->tinyInteger('type')->comment('类型（1:笔记 2:圈子 3:商品 4:用户 5:话题）');
                $table->decimal('degree_increment')->comment('热度增量');
                $table->index(['object_id', 'type'], 'index_0');
                $table->timestamps();
            });
        }
    }

    function _v1_0_20_1(){

//        if(file_exists(app_path('Extensions/laradocs/src/DcatNeditorServiceProvider.php'))){
//            if(!Extension::where('name', 'laradocs.dcat-neditor')->exists()){
//                $extension = new Extension();
//                $extension->name = 'laradocs.dcat-neditor';
//                $extension->version = '1.0.0';
//                $extension->is_enabled = 1;
//                $extension->save();
//            }
//            try{
//                if(!__system_is_model_enable('laradocs', 'dcat-neditor')){
//                    Admin::extension()->enable('laradocs.dcat-neditor');
//                }
//            }catch (\Exception $e){
//                _logger_(__file__, __line__, $e->getMessage());
//            }
//        }

    }

}
